/*
 * Copyright:  2018 smarabbit studio.
 *
 *  Licensed under the Confluent Community License; you may not use this file
 *  except in compliance with the License.  You may obtain a copy of the License at
 *
 *  http://www.confluent.io/confluent-community-license
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 *  WARRANTIES OF ANY KIND, either express or implied.  See the License for the
 *  specific language governing permissions and limitations under the License.
 *
 *  @作   者： 黄开晖 (117227773@qq.com)
 *  @日   期:  2021年9月9日
 */
package com.massyframework.beanskin.license.signature.wizard;

import java.util.function.UnaryOperator;

import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.control.TextFormatter;
import javafx.scene.control.TextFormatter.Change;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;

/**
 * 绑定客户控制器
 * @author huangkh
 *
 */
public class BindCustomerController extends Controller {

	@FXML
	private Button btnNext;
	
	@FXML
	private TextField txtCustomerId;
	@FXML
	private TextField txtCustomerName;


	/**
	 * 
	 */
	public BindCustomerController() {
	}

	@Override
	protected void init() {
		this.btnNext.setDisable(!this.isValidate());
		
		UnaryOperator<Change> filter = change -> {
		    String text = change.getText();
		    if (text.matches("[0-9]*")) {
		        return change;
		    }

		    return null;
		};
		
		TextFormatter<String> textFormatter = new TextFormatter<>(filter);
		this.txtCustomerId.setTextFormatter(textFormatter);
		this.txtCustomerId.setOnKeyPressed(this.pressedHandler);
		this.txtCustomerName.setOnKeyPressed(this.pressedHandler);
	}
	
	private EventHandler<KeyEvent> pressedHandler = event -> {
		if (event.getCode() == KeyCode.ENTER) {
			KeyEvent newEvent = 
				new KeyEvent(event.getSource(), event.getTarget(), event.getEventType(),
						event.getCharacter(), event.getText(), KeyCode.TAB,
						event.isShiftDown(), event.isControlDown(), event.isAltDown(), event.isMetaDown());
			event.consume();
			((TextField)event.getSource()).fireEvent(newEvent);
		}
		
		if (event.getCode() == KeyCode.TAB) {
			this.btnNext.setDisable(!this.isValidate());
		}
	};

	
	/**
	 * 输入项目是否齐全
	 * <p>
	 * 客户编号，客户名称是否不为null.
	 * @return
	 */
	protected boolean isValidate() {
		if (this.txtCustomerId.getText().trim().length() == 0) {
			return false;
		}
		
		if (this.txtCustomerName.getText().trim().length() == 0) {
			return false;
		}
		
		return true;
	}

	@Override
	public void destroy() {

	}
		
	@FXML
	protected void onNextClick(ActionEvent event) {
		if (this.txtCustomerId.getText().trim().length() != 0 &&
				this.txtCustomerName.getText().trim().length() != 0) {
			this.getWizard().getSignatureData().setCusstomerId(
					Long.parseLong(this.txtCustomerId.getText().trim()));
			this.getWizard().getSignatureData().setCustomerName(
					this.txtCustomerName.getText().trim());
						
			this.getWizard().doNext();		
		}
	}

}